package org.feng.fxfront.pages.components;

import javafx.util.StringConverter;
import org.feng.api.sys.constants.BaseEnum;

/**
 * 枚举和下拉框转换器
 *
 * @author feng
 */
public class ComboBoxBaseEnumConvert extends StringConverter<Integer> {

    private final Class<? extends BaseEnum> baseEnumClass;

    public ComboBoxBaseEnumConvert(Class<? extends BaseEnum> baseEnumClass) {
        this.baseEnumClass = baseEnumClass;
    }

    @Override
    public String toString(Integer code) {
        BaseEnum baseEnum = BaseEnum.convertToEnum(code, baseEnumClass);
        if(baseEnum == null) {
            return "请选择";
        }
        return baseEnum.getDesc();
    }

    @Override
    public Integer fromString(String desc) {
        BaseEnum baseEnum = BaseEnum.convertToEnum(desc, baseEnumClass);
        if(baseEnum == null) {
            return null;
        }
        return baseEnum.getCode();
    }
}
